/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry;

import io.sentry.util.Objects;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Deduplicates events containing throwable that has been already processed.
 *
 * @since 2021-06-02
 **/
public final class DuplicateEventDetectionEventProcessor implements EventProcessor {
    private final @NotNull Map<Throwable, Object> capturedObjects =
        Collections.synchronizedMap(new WeakHashMap<>());
    private final @NotNull SentryOptions options;

    /**
     * constructor
     *
     * @param options options
     */
    public DuplicateEventDetectionEventProcessor(final @NotNull SentryOptions options) {
        this.options = Objects.requireNonNull(options, "options are required");
    }

    @Override
    public @Nullable SentryEvent process(
        final @NotNull SentryEvent event, final @Nullable Object hint) {
        if (options.isEnableDeduplication()) {
            final Throwable throwable = event.getOriginThrowable();
            if (throwable != null) {
                if (capturedObjects.containsKey(throwable)
                    || containsAnyKey(capturedObjects, allCauses(throwable))) {
                    options
                        .getLogger()
                        .log(
                            SentryLevel.DEBUG,
                            "Duplicate Exception detected. Event %s will be discarded.",
                            event.getEventId());
                    return null;
                } else {
                    capturedObjects.put(throwable, null);
                }
            }
        } else {
            options.getLogger().log(SentryLevel.DEBUG, "Event deduplication is disabled.");
        }
        return event;
    }

    private static <T> boolean containsAnyKey(
        final @NotNull Map<T, Object> map, final @NotNull List<T> list) {
        for (T entry : list) {
            if (map.containsKey(entry)) {
                return true;
            }
        }
        return false;
    }

    private static @NotNull List<Throwable> allCauses(final @NotNull Throwable throwable) {
        final List<Throwable> causes = new ArrayList<>();
        Throwable ex = throwable;
        while (ex.getCause() != null) {
            causes.add(ex.getCause());
            ex = ex.getCause();
        }
        return causes;
    }
}
